<template>
  <el-container style="height: 100%;display: flex;flex-direction: column">
    <el-main class="main-select-body bgfff">
      <div class="table-bg"></div>
      <el-form inline>
        <el-form-item>
          <el-button type="primary" @click="$router.go(-1)">返回</el-button>
        </el-form-item>
      </el-form>
      <el-table
        class="marT20"
        :data="tableData"
        :height="300"
        v-loading="loading"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column width="70" align="center" type="index" label="序号"></el-table-column>
        <el-table-column
          v-for="item in columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          align="center"
        ></el-table-column>
        <el-table-column :label="$t('base.table.action')">
          <template slot-scope="scope">
            <el-button type="text" @click="dialogEdit(scope.row)">发布</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="changePageSize"
        @current-change="changePageNo"
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
      ></el-pagination>
      <el-dialog
        append-to-body
        width="600px"
        title="发布预设想定"
        :visible.sync="dialogValue"
        @close="emitClose"
      >
        <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="100px">
          <el-form-item label="发布想定类型">
            <el-radio-group v-model="ruleForm.releaseScenarioType">
              <el-radio label="0">公共想定</el-radio>
              <el-radio label="1">单方想定</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="ruleForm.releaseScenarioType == 1" label="接收方">
            <el-checkbox-group v-model="ruleForm.teamId">
              <el-checkbox
                v-for="item in participants"
                :label="item.id"
                :key="item.id"
              >{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="emitClose">取 消</el-button>
          <el-button type="primary" @click="emitSave">保 存</el-button>
        </span>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script>
import curd from "@/mixins/curd";
import { drillAPI as request, drillSettingAPI, drillDetailAPI } from "@/api";
import { getOrg, getUser } from "@/utils";
export default {
  mixins: [curd],
  data() {
    return {
      condition: {
        maneuverId: this.$route.params.id,
      },
      request: {
        page: request.releasexiangdingPage,
        publish: drillDetailAPI.publishXiangding,
      },
      columns: [
        { prop: "scenarioName", label: "想定名称" },
        { prop: "scenarioStatusText", label: "状态" },
        { prop: "scenarioCreateTime", label: "创建时间" },
      ],
      ruleForm: {
        scenarioId: "",
        releaseScenarioType: "0",
        teamId: [],
      },
      resetForm: {
        scenarioId: "",
        releaseScenarioType: "0",
        teamId: [],
      },
      participants: [],
    };
  },
  methods: {
    formatTableData(tableData) {
      tableData.forEach((el) => {
        el.scenarioStatusText = "";
        el.scenarioReleaseStatusText = "";
        if (el.scenarioStatus == 0) {
          el.scenarioStatusText = "禁用";
        } else if (el.scenarioStatus == 1) {
          el.scenarioStatusText = "启用";
        }

        if (el.scenarioReleaseStatus == 0) {
          el.scenarioReleaseStatusText = "未关联";
        } else if (el.scenarioReleaseStatus == 1) {
          el.scenarioReleaseStatusText = "已关联";
        }
      });
    },
    dialogEdit(item) {
      this.ruleForm = {
        maneuverId: this.$route.params.id,
        scenarioId: item.scenarioId,
        releaseScenarioType: "0",
        teamId: [],
      };
      drillSettingAPI.read(this.condition.maneuverId).then((res) => {
        this.participants = (res ? res.participants || [] : []).map((el) => {
          return {
            id: el.teamId,
            name: el.teamName,
          };
        });
      });
      this.dialogValue = true;
    },
    emitSave() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.request
            .publish({
              ...this.ruleForm,
              teamId:
                this.ruleForm.releaseScenarioType == 1
                  ? this.ruleForm.teamId.join(",")
                  : "",
            })
            .then((res) => {
              this.initData();
              this.emitClose();
            });
        }
      });
    },
  },
};
</script>

<style>
</style>
